/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.data.controller.DistributionSemenCtlr;
import org.gov.vis.data.controller.DistributionSemenCtlrImpl;
import org.gov.vis.data.dto.DistributionSemenDto;
import org.gov.vis.data.gui.EditDistributionSemenDialog;
import org.gov.vis.data.gui.ViewDistributionSemenDialog;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ViewDistributionSemenListener implements ActionListener,BaseListener {

	


	
	private DistributionSemenCtlr distributionSemenCtlr = new DistributionSemenCtlrImpl();
	private ViewDistributionSemenDialog viewDistributionSemenDialog;
	private DistributionSemenDto distributionSemenDto;
	final private ResourceBundle rb = Util.getResources();
	private EditDistributionSemenDialog editDistributionSemenDialog;

	
	public ViewDistributionSemenListener(ViewDistributionSemenDialog viewDistributionSemenDialog) {
		// TODO Auto-generated constructor stub
		this.viewDistributionSemenDialog = viewDistributionSemenDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewDistributionSemenDialog _viewDistributionSemenDialog =(ViewDistributionSemenDialog)_guiObject;
		distributionSemenDto = new DistributionSemenDto();
		
		String code = _viewDistributionSemenDialog.getCodeTxtBx().getText();
		if(code!=null){
		 distributionSemenDto.setCode(code);
		}
		/*otherDto.setNameOfOrg(_othersDialog.getTxtFldOrg().getText());
		otherDto.setNameOfAIWorker(_othersDialog.getTxtFldAIWorker().getText());
		otherDto.setHeadQuarter(_othersDialog.getTxtFldHeadQuarter().getText());
		otherDto.setJuridiction(_othersDialog.getTxtFldJurisdiction().getText());
		otherDto.setMandal(_othersDialog.getTxtFldMandal().getText());
		otherDto.setDistrict(_othersDialog.getTxtFldDistrict().getText());*/
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		final DistributionSemenDto _distributionSemenDto = (DistributionSemenDto)_dtoObject;
		if(editDistributionSemenDialog != null){
			editDistributionSemenDialog.getTxtFldDSBreed().setText(_distributionSemenDto.getBreed());
			editDistributionSemenDialog.getTxtFldDSBullNo().setText(_distributionSemenDto.getBullNo());
			editDistributionSemenDialog.getTxtFldDSCode().setText(_distributionSemenDto.getCode());
			editDistributionSemenDialog.getTxtFldDSDiscord().setText(_distributionSemenDto.getDiscord());
			editDistributionSemenDialog.getDtChoDateOfDistribution().setDate(_distributionSemenDto.getDoDstrbut());
			editDistributionSemenDialog.getTxtFldDSNoOfStraws().setText(_distributionSemenDto.getNoStrawas());
			editDistributionSemenDialog.getTxtFldDSValue().setText(_distributionSemenDto.getValue());
			editDistributionSemenDialog.setHidnObject(_distributionSemenDto.getDisSemenId());
		}

	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub
		
		
		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewDistributionSemenDialog);
			try {
				
			List<DistributionSemenDto> distributionSemenLst = distributionSemenCtlr.searchDisSemens(distributionSemenDto);
			
			List<String> colLst = new ArrayList<String>();
			colLst.add("Code");
			colLst.add("Date of Distribution");
			//colLst.add("Breed");
			colLst.add("Bul No");
			//colLst.add("Value");
			
			ArrayList<Object> dataList = new ArrayList<Object>(1);
			if(distributionSemenLst != null && !distributionSemenLst.isEmpty()){
				for(DistributionSemenDto dto:distributionSemenLst){
					ArrayList<Object> lst = new ArrayList<Object>(1);
					//setting each property of the current Accessory object (row) to the list
					lst.add(dto);
					lst.add(dto.getCode());
					lst.add(dto.getDoDstrbut());
				//	lst.add(dto.getBreed());
					lst.add(dto.getBullNo());
					//lst.add(dto.getValue());
					dataList.add(lst);
				}//end for 
			}//end if
			VISDataGrid.createDataPanel(dataList, colLst, viewDistributionSemenDialog.getPnlDistributionSemenResults(),this);
			
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			DistributionSemenDto distributionSemenDto = (DistributionSemenDto)visHyperLink.getObject();
			//System.out.println("action from the object----"+distributionSemenDto.getCode());
			editDistributionSemenDialog = new EditDistributionSemenDialog(Main.getMainFrame(),
                    rb.getString("dialog_edit_dist_semen_title"),
                    false);
			setDtoDetailsToGui(distributionSemenDto);
			viewDistributionSemenDialog.dispose();

		}
		
	}







}
